/*******************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
 * @file     uiQRCode.c
 * @author   UartTFT Application Team
 * @version  V0.0.1
 * @date     2023-01-11
 * @brief    This file provides UartTFT Application display function
 ********************************************************************************/
#include "uiQRCode.h"
#include "qr_encode.h"
#include "levetop.h"
#include "data.h"
#include "rtc.h"
#include "keyBoard.h"

extern uint8_t Pop_up_flag;
extern uint16_t Pop_up_x;
extern uint16_t Pop_up_y;

/***********************************************************************************
* Function Name : qr_code
* Description   : Show QRcode.
* Input         : - type: QRcode info sources
				  - buff: QRcode info
* Output        : None
* Return        : None
************************************************************************************/
void qr_code(uint8_t type, uint8_t *buff)
{
	uint16_t p_add, pointer, length;
	uint16_t i = 0, j = 0;
	uint16_t x1 = 0, y1 = 0, x, y;
	int32_t size = 49;
	uint8_t dxy = 0;
	uint8_t unit_pixle;
	uint16_t backgroud_size = 50; // White background magnification base

	if (type == 0) // instruction information source is configuration file
	{
		p_add = (buff[5] + (buff[6] << 8));
		length = buff[7] + (buff[8] << 8);
		x = buff[9] + (buff[10] << 8);
		y = buff[11] + (buff[12] << 8);
		unit_pixle = buff[13];
	}
	else if (type == 1) // instruction information ource is variable space
	{
		pointer = (buff[2] + (buff[3] << 8)) * 2;
		p_add = (var[pointer + 1] + (var[pointer + 2] << 8));
		length = var[pointer + 3] + (var[pointer + 4] << 8);
		x = var[pointer + 5] + (var[pointer + 6] << 8);
		y = var[pointer + 7] + (var[pointer + 8] << 8);
		unit_pixle = var[pointer + 9];
	}
	Var_AddrRemapping(&p_add);
	p_add = p_add*2;

	if (Pop_up_flag == 1) // Popup offset
	{
		x += Pop_up_x;
		y += Pop_up_y;
	}

	EncodeData(&var[p_add]);

	// m_nSymbleSize: It's the real QR code size
	size = size * unit_pixle / m_nSymbleSize;						// Size of a single black pixel of the actual QR code
	dxy = (backgroud_size * unit_pixle - m_nSymbleSize * size) / 2; // The actual QR code is at the beginning of the white background
	y1 = y + dxy;

	LT268_TFT_FillColor(x, y, backgroud_size * unit_pixle, backgroud_size * unit_pixle, White); // Enlarged white background size

	// display
	if (Screen_DIR == 0)
	{
		for (i = 0; i < m_nSymbleSize; i++)
		{
			x1 = dxy + x;
			for (j = 0; j < m_nSymbleSize; j++)
			{
				if (m_byModuleData[i][j])
				{
					LT268_TFT_FillColor(x1, y1, size, size, Black);
				}
				x1 += size;
			}
			y1 += size;
		}
	}
	else if (Screen_DIR == 1)
	{
		for (j = 0; j < m_nSymbleSize; j++)
		{
			x1 = dxy + x;
			for (i = 0; i < m_nSymbleSize; i++)
			{
				if (m_byModuleData[m_nSymbleSize - 1 - i][j])
				{
					LT268_TFT_FillColor(x1, y1, size, size, Black);
				}
				x1 += size;
			}
			y1 += size;
		}
	}
	else if (Screen_DIR == 2)
	{
		for (i = 0; i < m_nSymbleSize; i++)
		{
			x1 = dxy + x;
			for (j = 0; j < m_nSymbleSize; j++)
			{
				if (m_byModuleData[m_nSymbleSize - 1 - i][m_nSymbleSize - 1 - j])
				{
					LT268_TFT_FillColor(x1, y1, size, size, Black);
				}
				x1 += size;
			}
			y1 += size;
		}
	}
	else if (Screen_DIR == 3)
	{
		for (j = 0; j < m_nSymbleSize; j++)
		{
			x1 = dxy + x;
			for (i = 0; i < m_nSymbleSize; i++)
			{
				if (m_byModuleData[i][m_nSymbleSize - 1 - j])
				{
					LT268_TFT_FillColor(x1, y1, size, size, Black);
				}
				x1 += size;
			}
			y1 += size;
		}
	}
}
